package com.shop.servlet.product;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.tomcat.util.http.fileupload.IOUtils;

import com.shop.utils.DBUtil;
import com.shop.utils.Upload;

public class AddProduct extends HttpServlet {
	private static final long serialVersionUID = 1L;
    public AddProduct() {
        super();
    }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setHeader("Content-Type","text/html;charset=utf-8");
		response.setContentType("text/html;charset=utf-8");	
try{
			
			
			/*System.out.println(request.getParameter("pImage"));
			System.out.println(request.getParameterNames());
			 // 1.创建磁盘文件项工厂----这里的temp为临时文件目录
            DiskFileItemFactory factory = new DiskFileItemFactory(1024 * 1024, new File("c://temp"));
            // 2.创建文件上传的核心类
            ServletFileUpload upload = new ServletFileUpload(factory);
         // 设置上传文件名的编码----以防文件名乱码
            upload.setHeaderEncoding("utf-8");
            boolean multipartContent = ServletFileUpload.isMultipartContent(request);// 判断表单是否是文件上传的表单
            if (multipartContent) {
            	  // 是文件上传的表单
                // **解析request获得文件项集合
                List<FileItem> parseRequest = upload.parseRequest(request);
                if (parseRequest!=null) {
             for (FileItem item : parseRequest) {
			//拿到源文件名称
			String fileName=request.getParameter("pImage");
			Name=fileName;
            //得到输入流
            InputStream in = item.getInputStream();
            //获取文件存放路径
            String path = "D:\\Workspace\\ShopSystem-master\\WebContent\\image\\";
            //将文件存入相应目录
            OutputStream out = new FileOutputStream(path+fileName);
            IOUtils.copy(in, out);
            in.close();
            out.close();
            //最后删除temp目录下的临时文件、
            item.delete();
                    }
                    }
            }*/
		/*	DiskFileItemFactory factory = new DiskFileItemFactory();
			//2、创建一个文件上传解析器
	        ServletFileUpload upload = new ServletFileUpload(factory);
	        List<String> pList = new ArrayList<>();
			List<FileItem> list = null;
			String filename=null;
			
				list = (List<FileItem>) request.getParameterNames();
			
			for(FileItem item : list){
	            //如果fileitem中封装的是普通输入项的数据
	            if(item.isFormField()){
			
	            	continue;
	            }else{//如果fileitem中封装的是上传文件
	            	InputStream stream=item.getInputStream();//上传文件需要的文件流参数
	            	filename=item.getName();   //上传文件需要的参数
	            	
	            	
	            	String savepath=getServletContext().getRealPath("");
	                File path=new File("");  //这个要自己写具体的路径，是需要上传文件需要的参数
	                Upload.uploadFile(stream, path,filename);   //调用工具类方法
	                if(filename==null || filename.trim().equals("")){
	                //判空处理}
	                continue;
	            }
			}*/
		/************************************图片上传*****************************************/

			PrintWriter out = response.getWriter();
			String pName = request.getParameter("pName");
			String pDesc = request.getParameter("pDesc");
			int pNum = Integer.parseInt(request.getParameter("pNum"));
			String  pubTime = (request.getParameter("pubTime"));
			int pKeepTime = Integer.parseInt(request.getParameter("pKeepTime"));
			String pImage="image/"+request.getParameter("pImage");
			System.out.println(request.getParameter("kId"));
			int kId = Integer.parseInt(request.getParameter("kId"));
			BigDecimal iPrice = new BigDecimal(Double.valueOf(request.getParameter("iPrice")));
			BigDecimal mPrice = new BigDecimal(Double.valueOf(request.getParameter("mPrice")));
			String isHot = request.getParameter("isHot");
			String isShow = request.getParameter("isShow");
			
	        //开始解析  
	        
			String sql = "insert into product(pName,pDesc,pNum,pubTime,pKeepTime,pImage,kId,iPrice,mPrice,isHot,isShow) values(?,?,?,?,?,?,?,?,?,?,?)";
			Object[] params = {
			    pName, 
			    pDesc ,
				pNum,
				pubTime,
				pKeepTime,
				pImage,
				kId,
			    iPrice,
				mPrice ,
				isHot,
				isShow
			};
			DBUtil db = new DBUtil();
			int n = db.doUpdate(sql, params);
			if(n > 0){
				out.println("<script>alert('添加成功！')</script>");
			}else {
				out.println("<script>alert('添加失败！')</script>");
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}

}
